<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>博客主页</title>
    <link rel="stylesheet" href="css/common.css">
    <link rel="stylesheet" href="css/myblog_list.css">
	
	<script src="js/jquery.min.js"></script>
	<script src="js/logout.js"></script>
</head>
<body>
    <div class="nav">
        <div class="flag">
            <img src="image/face2.png" alt="logo">
            <div class="title">我的博客系统</div>
        </div>

        <div class="page">
            <a href="blog_list.html">主页</a>
			<a href="myblog_list.html">内容管理</a>
            <a href="blog_add.html">写博客</a>
			<a href="javascript:logout()">注销</a>

        </div>
    </div>
    
    <div class="container">
        <div class="left">
            <div class="card">
                <img src="image/face2.png" alt="face" id="photo">
                <h3 id="username">不摸鱼的程序员</h3>
                <a href="https://gitee.com/">gitee地址</a>
                <div class="conter">
                    <span>文章</span>
                </div>
                <div class="conter">
                    <span id="artcount">0</span>
                </div>
            </div>
        </div>
        <div class="right">
            <div class="blog">
<!--                <h3>我的第一篇博客</h3>
                <div class="date">2022-4-26 14:22:00</div>
                <div class = "desc">编程之路，道阻且长。Lorem ipsum dolor sit amet consectetur adipisicing elit. 
                    Quas eaque, blanditiis sunt praesentium ullam laudantium esse odit assumenda 
                    commodi molestias illo provident quis nisi, veritatis pariatur! Commodi rerum 
                    vitae beatae.
                </div>
				<div class="update">
					<a href="blog_content.html?id=1" class="detail">查看全文 &gt;&gt;</a>&nbsp;&nbsp;
					<a href="blog_content.html?id=1" class="detail">修改 &gt;&gt;</a>&nbsp;&nbsp;
					<a href="blog_content.html?id=1" class="detail">删除 &gt;&gt;</a>
				</div>
            </div> -->
            
        </div>
    </div>
	<script>
	        // 初始化页面方法
	        function init() {
	            jQuery.ajax({
	                // 1.向后端请求数据
	                url:"/art/mylist",
	                type:"GET",
	                data:{},
	                success:function(res) {
	                    if (res.code == 200 && res.data != null) {
	                        // 请求成功
	                        var createHtml = "";
	                        var articles = res.data.arts;
	                        if (articles == null || articles.length == 0) {
	                            createHtml += "<h3 style='text-align: center;margin-top:10px'>暂未发表文章<h3>";
	                        } else {
	                            for(var i=0;i<articles.length;i++){
	                                var art = articles[i];
	                                createHtml += '<div class="blog">';    
	                                createHtml += '<h3>'+art.title+'</h3>'; 
	                                createHtml += '<div class="date">'+art.createtime+'</div>';
	                                createHtml += '<div class="desc">';
	                                createHtml += art.content;
	                                createHtml += '</div>';
	                                createHtml += '<div class="update">';
	                                createHtml += ' <a href="blog_content.html?aid='+
	                                    art.id + '" class="detail">查看全文 &gt;&gt;</a>&nbsp;&nbsp;';
	                                createHtml += '<a href="blog_edit.html?aid='+
	                                    art.id + '" class="detail">修改 &gt;&gt;</a>&nbsp;&nbsp;';
	                                createHtml += ' <a href="javascript:del('+art.id+')" class="detail">删除 &gt;&gt;</a>';
	                                createHtml += '</div>';
	                                createHtml += '</div>';
	                            }
	                        }
	                        jQuery(".right").html(createHtml);
	                        
	                        var user = res.data.user;
	                        if (user != null) {
	                            if (user.photo != "") {
	                                jQuery("#photo").attr("src",user.photo);
	                            }
	                            jQuery("#username").html(user.username);
	                            jQuery("#artcount").html(user.artCount);
	                        } else {
	                            alert("查询失败:"+res.meg);
	                        }
	
	                    }else{
	                        alert("操作失败！"+res.meg);
	                    }
	                }
	            });
	        }
	        // 调用方法
	        init();
	
	        function del(aid) {
	            if (!confirm("确定删除次文章？")) {
	                return false;
	            }
	            // 1.参数校验
	            if (aid == "" || aid <= 0) {
	                alert("参数错误!");
	            }
	            // 2.将数据提交给后端
	            jQuery.ajax({
	                url:"/art/del",
	                type:"POST",
	                data:{
	                    "id":aid
	                },
	                success:function(res){
	                    // 3.将结果返回给用户
	                    if (res.code == 200 && res.data == 1) {
	                        // 删除成功
	                        alert("删除成功!");
	                        // 刷新页面
	                        location.href = location.href;
	                    } else {
	                        // 删除失败
	                        alert("删除失败!"+res.meg);
	                    }
	                }
	            });
	        }
	    </script>
</body>
</html>